<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%!
public Map cate(List<Map> list, Map dest, List<Map> _dest) {
    List<Map> subs = (List<Map>)dest.get("depts");
    if (subs == null) {
      subs = new ArrayList<Map>();
      dest.put("depts", subs);
    }
    String _parent = Tools.getDesc(dest.get("dept_id")+"", "-1001");
    Iterator<Map> iterator = list.iterator();
    while (iterator.hasNext()) {
      Map map = iterator.next();
      String parent = Tools.getDesc(map.get("parent")+"", "-1000");
      if (_parent.equals(parent)) {
        iterator.remove();
        _dest.add(map);
        List<Map> _list = new ArrayList<Map>();
        _list.addAll(list);
        subs.add(cate(_list, map, _dest));
      }
    }
    dest.put("size", subs.size() + "");
    return dest;
}
%>
<%
    String parent = input.getDesc("parent", "-1");
    Integer branch = input.getInt("branch", -1);
    Integer type = input.getInt("type", DEPT_ALL);  // 不传默认科室介绍DEPT_ALL
    String selectContent = "new map(id as id , deptId as dept_id, deptName as dept_name, parent as parent, branch as branch, branchName as branch_name, type as type)";
    Where where  = new Where();
    if (branch != -1) {
        where.and("branch=", branch);
    }
    if (DEPT_REGISTER.equals(type)) {
        where.and("type in("+DEPT_ORDINARY+","+DEPT_EXPERT+")");
    } else {
        where.and("type=", type);
    }
    List<Map> list = QBuilder.db("basedb").select(selectContent).from("Dept").where(where.and("hospital=", hospital).and("status = ", STATUS_ENABLE)).order("hospitalOrder", "asc").query().getResultObj();
    if (list.size() == 0) {
        msg = new Message(30041, "没有相关数据");
        msg.put("data", list);
    } else {
        Map<String, List<Map>> map = new HashMap<String, List<Map>>();
        for (int i = 0; i < list.size(); i++) {
            Map dept = list.get(i);
            String _branch = Tools.getDesc(dept.get("branch"), "1");
            String branchName = Tools.getDesc(dept.get("branch_name"), "本部");
            List<Map> details = map.get(branchName + "_" + _branch);
            if (details == null) {
                details = new ArrayList<Map>();
                map.put(branchName + "_" + _branch, details);
            }
            details.add(dept);
        }
        Set<Entry<String, List<Map>>> set = map.entrySet();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (Entry<String, List<Map>> entry : set) {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            result.add(resultMap);
            String key = entry.getKey();
            List<Map> values = entry.getValue();
            String[] keys = key.split("_");
            resultMap.put("branch_name", keys[0]);
            resultMap.put("branch_id", keys.length == 2 ? keys[1] : "");
            resultMap.put("size", 0);
            if (isEmpty(values)) {
                resultMap.put("msg", "暂无相关数据");
                resultMap.put("depts", new ArrayList<Map>());
                continue;
            }
            if (DEPT_REGISTER.equals(type)) {    // 如果是普通专家科室一起返回归类
                Iterator<Map> iterator = values.iterator();
                List<Map> _values = new ArrayList<Map>();
                while (iterator.hasNext()) {
                    boolean isAdd = true;
                    Map tempMap = iterator.next();
                    String deptName = Tools.getDesc(tempMap.get("dept_name"), "");
					String dept_Id = Tools.getDesc(tempMap.get("dept_id"), "");
                    String tempParent = Tools.getDesc(tempMap.get("parent"), "");
                    for (int i = 0; (! ("-1".equals(tempParent))) && i < _values.size(); i++) {
                        Map _map = _values.get(i);
                        String _deptName = Tools.getDesc(_map.get("dept_name"), "");
                        String _parent = Tools.getDesc(_map.get("parent"), "");
						String _deptId = Tools.getDesc(_map.get("dept_id"), "");
                        if (dept_Id.equals(_deptId) && tempParent.equals(_parent)) {     // 根据科室id判断
                            isAdd = false;
                            String deptType = Tools.getDesc(tempMap.remove("type"), "");
                            String deptId = Tools.getDesc(tempMap.get("dept_id"), "");
                            String id = Tools.getDesc(tempMap.remove("id"), "");
                            Map registerMap = (Map)_map.get("registers");
                            if (registerMap == null) {
                                registerMap = new HashMap();
                                _map.put("registers", registerMap);
                            }
                            String _key = (DEPT_ORDINARY+"").equals(deptType) ? "normal" : "expert"; // 判断是否专家号
                            List deptList = (List)registerMap.get(_key);
                            if (deptList == null) {
                                deptList = new ArrayList();
                                registerMap.put(_key, deptList);
                            }
                            Map deptMap = new HashMap();
                            deptMap.put("dept_id", deptId);
                            deptMap.put("type", deptType);
                            deptMap.put("id", id);
							deptMap.put("dept_name", deptName);
                            deptList.add(deptMap);
                            break;
                        }
                    }
                    if (isAdd) {
                        String _parent = Tools.getDesc(tempMap.get("parent") + "", "");
                        if (!"-1".equals(_parent)) {
                            String deptType = Tools.getDesc(tempMap.remove("type"), "");
                            String deptId = Tools.getDesc(tempMap.get("dept_id"), "");
                            String id = Tools.getDesc(tempMap.remove("id"), "");
                            Map registerMap = (Map)tempMap.get("registers");
                            if (registerMap == null) {
                                registerMap = new HashMap();
                                tempMap.put("registers", registerMap);
                            }
                            String _key = (DEPT_ORDINARY+"").equals(deptType) ? "normal" : "expert";  // 判断是否专家号
                            List deptList = (List)registerMap.get(_key);
                            if (deptList == null) {
                                deptList = new ArrayList();
                                registerMap.put(_key, deptList);
                            }
                            Map deptMap = new HashMap();
                            deptMap.put("dept_id", deptId);
                            deptMap.put("type", deptType);
                            deptMap.put("id", id);
							deptMap.put("dept_name", deptName);
                            deptList.add(deptMap);
                            _values.add(tempMap);
                        } else {       // 父类去重
                            boolean _isAdd = true;
                            for (int i = 0; i < _values.size(); i++) {
                                Map _map = _values.get(i);
                                String _deptName = Tools.getDesc(_map.get("dept_name"), "");
                                if (deptName.equals(_deptName)) {
                                    _isAdd = false;
                                    break;
                                }
                            }
                            if (_isAdd) {
                                _values.add(tempMap);
                            }
                        }
                    }
                }
                if (_values.size() > 0) {
                    values = _values;
                }
            }
            List<Map> dest = new ArrayList<Map>();
            List<Map> _dest = new ArrayList<Map>();
            Iterator<Map> iterator = values.iterator();
            while (iterator.hasNext()) {
              Map dept = iterator.next();
              dept.put("size", "0");
              dept.put("depts", new ArrayList<Map>());
              String _parent = Tools.getDesc(dept.get("parent")+"", "-1");
              if (parent.equals(_parent)) {
                iterator.remove();
                _dest.add(dept);
                List<Map> _list = new ArrayList<Map>();
                _list.addAll(values);
                dest.add(cate(_list, dept, _dest));
              }
            }
            values.removeAll(_dest);
            dest.addAll(values);
            resultMap.put("size", dest.size());
            resultMap.put("depts", dest);
        }
        msg.put("data", result);
    }
    Tools.encryptPrint(out, msg.toJson());
%>
